package com.wzm.order.service.impl;

import com.wzm.basic.query.BaseQuery;
import com.wzm.basic.util.LoginContext;
import com.wzm.basic.util.PageList;
import com.wzm.order.domain.OrderDto;
import com.wzm.order.domain.OrderPetAcquisition;
import com.wzm.order.mapper.OrderPetAcquisitionMapper;
import com.wzm.order.query.OrderPetAcquisitionQuery;
import com.wzm.order.service.IOrderPetAcquisitionService;
import com.wzm.basic.service.impl.BaseServiceImpl;
import com.wzm.org.domain.Employee;
import com.wzm.org.domain.Shop;
import com.wzm.org.mapper.EmployeeMapper;
import com.wzm.org.mapper.ShopMapper;
import com.wzm.pet.domain.Pet;
import com.wzm.pet.domain.SearchMasterMsg;
import com.wzm.pet.mapper.PetMapper;
import com.wzm.pet.mapper.SearchMasterMsgMapper;
import com.wzm.user.domain.Logininfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * 业务实现类：
 */
@Service
public class OrderPetAcquisitionServiceImpl extends BaseServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
    @Autowired
    private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private SearchMasterMsgMapper searchMasterMsgMapper;
    @Autowired
    private PetMapper petMapper;
    @Override
    public PageList<OrderPetAcquisition> queryPageByRole(OrderPetAcquisitionQuery query, HttpServletRequest request) {
        //获取登录信息对象
        Logininfo logininfo = LoginContext.getLogininfo(request);
        //获取管理员信息
        Employee employee = employeeMapper.loadByLogininfoId(logininfo.getId());
        //获取店铺id
        Long shop_id = employee.getShop_id();
        if(shop_id!=null){//店铺管理员 = 店长 or 店员
            //将店铺id设置到query
            query.setShopId(shop_id);
            //获取店铺的对象
            Shop shop = shopMapper.loadById(shop_id);
            //shop.getAdminId() = employee_id
            //如果店铺管理员id 不等于 当前登录人的id
            if(!shop.getAdmin_id().equals(employee.getId())){//店员
                //店员只能查询自己处理的寻主消息
                query.setEmpId(employee.getId());
            }
        }
        return super.queryPage(query);
    }

    @Override
    public void confirm(OrderDto orderDto) {
        Long orderId = orderDto.getId();
        BigDecimal money = orderDto.getMoney();
        Integer paytype = orderDto.getPaytype();
        //1.修改订单状态：state：0待处理 - 1已处理。价格：要以现在的【收购宠物时】为主，用户寻主消息的价格不是最终的价格 ，支付方式
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(orderId);
        order.setState(1);
        order.setPrice(money);
        order.setPaytype(paytype);
        orderPetAcquisitionMapper.update(order);

        //2.修改寻主消息状态：state：1待处理 - 2处理完成。价格：可以不改【寻主消息的金额可以与最终价格不一样】
        Long msgId = order.getSearchMasterMsgId();
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(msgId);
        msg.setState(2);
        searchMasterMsgMapper.update(msg);

        //3.信息宠物金额：价格：以线下收购时的价格为主
        Long petId = order.getPetId();
        Pet pet = petMapper.loadById(petId);
        pet.setCostprice(money);
        petMapper.update(pet);
    }

    @Override
    public void del(Long id) {
        OrderPetAcquisition order = orderPetAcquisitionMapper.loadById(id);
        //1.将宠物放到寻主池：state = 1，shopId置空
        Long msgId = order.getSearchMasterMsgId();
        SearchMasterMsg msg = searchMasterMsgMapper.loadById(msgId);
        msg.setShopId(null);
        searchMasterMsgMapper.update(msg);

        //2.删除宠物信息
        petMapper.remove(order.getPetId());

        super.del(id);//删除订单
    }
}
